package cn.gailvlun.gll.presentation.base;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.gailvlun.gll.event.AuthEvent;
import cn.gailvlun.gll.mgr.ShortcutMgr;
import cn.gailvlun.gll.presentation.login.LoginActivity;



public abstract class BaseActivity extends RxAppCompatActivity {

    protected BaseActivity mContext;
    protected boolean isDestroyed = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        mContext = this;

        EventBus.getDefault().register(this);

        initComponent();
        createHandler();
        loadData(savedInstanceState);
    }

    
    protected abstract void initComponent();

    
    protected abstract void createHandler();

    
    protected abstract void loadData(@Nullable Bundle savedInstanceState);

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        isDestroyed = true;
    }

    public boolean isDestroyed() {
        return isDestroyed;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAuthEvent(AuthEvent event) {
        if (ActivityUtils.getTopActivity() == this) {
            ShortcutMgr.logOut(mContext);
            
            LoginActivity.openActivity(mContext, true);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(mContext);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(mContext);
    }
}
